#encoding:utf8
import os , sys , shutil
import filecmp
try:
	from py_ex import *
except:
	import logging

kPdifType = ["abs" , "relative" , "debug"]
def chkFiledif(oldpath , newpath , pathtype = "abs"):
	#init filepath
	oldpath_list = dict()
	for obj1 in os.walk(oldpath):
		oldpath_list[obj1[0].replace(oldpath , 'base')] = {"files":obj1[2]}

	newpath_list = dict()
	for obj2 in os.walk(newpath):
		newpath_list[obj2[0].replace(newpath , 'base')] = {"files":obj2[2]}


	retabs = dict(
		path = [],
		files = []
		)
	retrelative = dict(
		path = [],
		files = []
		)
	retdebug = dict(
		path = [],
		files = []
		)
	#compare dir
	for k , v in newpath_list.items():
		if not oldpath_list.has_key(k):

			path_abs      = k.replace("base" , newpath)
			path_relative = k.replace("base" ,'')
			if path_relative.startswith(os.sep):
				path_relative = path_relative.replace(os.sep , '' , 1)
			
			retabs["path"].append(path_abs)
			retrelative["path"].append(path_relative)
			retdebug["path"].append(path_abs+"|add")
			
			logging.info("added path-->[%s] ..."%(path_abs))
		else:
			for filename in v["files"]:
				#single file
				fileabsold = os.path.join(k.replace("base" , oldpath) , filename)
				fielabsnew = os.path.join(k.replace("base" , newpath) , filename)

				fielrelativenew = os.path.join(k.replace("base" , '') , filename)	
				if fielrelativenew.startswith(os.sep):
					fielrelativenew = fielrelativenew.replace(os.sep , '' , 1)			

				if oldpath_list[k]["files"].count(filename) == 0:
					#debug
					logging.info("added file-->[%s] ..."%(fielabsnew))
					retabs["files"].append(fielabsnew)
					retrelative["files"].append(fielrelativenew)
					retdebug["files"].append(fielabsnew+"|add")
				else:
					if filecmp.cmp(fielabsnew , fileabsold) == False:
						logging.info("changed file-->[%s] ..."%(fielabsnew))
						#debug
						retabs["files"].append(fielabsnew)
						retrelative["files"].append(fielrelativenew)
						retdebug["files"].append(fielabsnew+"|chg")

	if pathtype == "abs":
		return retabs
	elif pathtype == "relative":
		return retrelative
	elif pathtype == "debug":
		return retdebug